package com.learn.sb.sblearnshardingjdbc.strategy;

import io.shardingjdbc.core.api.algorithm.sharding.PreciseShardingValue;
import io.shardingjdbc.core.api.algorithm.sharding.ShardingValue;
import io.shardingjdbc.core.api.algorithm.sharding.complex.ComplexKeysShardingAlgorithm;

import java.util.Collection;
import java.util.HashSet;


public class UseridAndOrderidShardingAlgorithm implements ComplexKeysShardingAlgorithm {

	@Override
	public Collection<String> doSharding(Collection<String> availableTargetNames, Collection<ShardingValue> shardingValues) {
		Collection<String> routeTables = new HashSet<>(); // 存放结果

		String databasePostfix;
		String tablePostfix;
		if (null != shardingValues) {
			for (ShardingValue shardingValue : shardingValues) {
				// 遍历 Collection<T extends [logicTableName, columnName]>
				if (shardingValue instanceof PreciseShardingValue) {
					// eq 条件
					PreciseShardingValue preciseShardingValue = (PreciseShardingValue) shardingValue;
					Comparable value = preciseShardingValue.getValue();
					String logicTable = preciseShardingValue.getLogicTableName();
					if ("user_id".equals(preciseShardingValue.getColumnName())) {
						databasePostfix = "_" + (Integer)preciseShardingValue.getValue() % 2;
					} else if ("order_id".equals(preciseShardingValue.getColumnName())) {
						tablePostfix = "_" + (Integer)preciseShardingValue.getValue() % 2;
					}
				}
			}
		}
		return null;
	}

}
